package bigoo.ds.message;

import bigoo.common.communication.AbstractMessage;
import bigoo.common.communication.InvalidMessageException;
import bigoo.common.communication.Message;
import bigoo.common.communication.MessageBuilder;

public class ImAliveAnswerMessage extends AbstractMessage {

    public static final String MSG_IDENTIFIER = "IM_ALIVE_ANSWER";

    private int lease;
    private int threshold;

    public ImAliveAnswerMessage(String id, int lease, int threshold) {
	super(id, MSG_IDENTIFIER);
	this.lease = lease;
	this.threshold = threshold;
    }

    @Override
    public String getRawMessage() {
	return this.getSenderID() + SEPARATOR + MSG_IDENTIFIER + SEPARATOR + lease + PARAM_SEPARATOR + threshold + END_OF_MESSAGE;
    }

    public int getLease() {
	return lease;
    }

    public int getThreshold() {
	return threshold;
    }

    public static class Builder implements MessageBuilder {
	@Override
	public Message parseMessage(String rawMessage) throws InvalidMessageException {
	    String senderID = parseSenderID(rawMessage);
	    String[] params = parseListParams(rawMessage);
	    String[] param = parseParams(params[0]);
	    int lease = Integer.parseInt(param[0]);
	    int threshold = Integer.parseInt(param[1]);
	    return new ImAliveAnswerMessage(senderID, lease, threshold);
	}

    }
}
